package cn.cscamp.gateway.session.defaults;

import cn.cscamp.gateway.datasource.DataSource;
import cn.cscamp.gateway.datasource.DataSourceFactory;
import cn.cscamp.gateway.datasource.unpooled.UnpooledDataSourceFactory;
import cn.cscamp.gateway.executor.Executor;
import cn.cscamp.gateway.session.Configuration;
import cn.cscamp.gateway.session.GatewaySession;
import cn.cscamp.gateway.session.GatewaySessionFactory;

/**
 * 泛化调用会话工厂
 *
 * @author whx
 */
public class DefaultGatewaySessionFactory implements GatewaySessionFactory {

    private final Configuration configuration;

    public DefaultGatewaySessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public GatewaySession openSession(String uri) {
        DataSourceFactory dataSourceFactory = new UnpooledDataSourceFactory();
        dataSourceFactory.setProperties(configuration, uri);
        DataSource dataSource = dataSourceFactory.getDataSource();
        // 创建执行器
        Executor executor = configuration.newExecutor(dataSource.getConnection());
        return new DefaultGatewaySession(configuration, executor, uri);
    }
}
